package io.chenglicun.modules.clc.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.entity.BaseEntity;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.modules.clc.dao.ClcUserIdentityDao;
import io.chenglicun.modules.clc.dto.ClcUserIdentityDTO;
import io.chenglicun.modules.clc.entity.ClcUserIdentityEntity;
import io.chenglicun.modules.clc.service.ClcUserIdentityService;
import io.chenglicun.modules.security.user.SecurityUser;
import io.chenglicun.modules.sys.dao.SysDictDataDao;
import io.chenglicun.modules.sys.entity.SysDictDataEntity;
import io.chenglicun.modules.sys.entity.SysUserPostEntity;
import io.chenglicun.modules.sys.service.SysDictDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户身份关联表
 *
 * @author HB 137664951@qq.com
 * @since 1.0 2023-10-30
 */
@Service
public class ClcUserIdentityServiceImpl extends CrudServiceImpl<ClcUserIdentityDao, ClcUserIdentityEntity, ClcUserIdentityDTO> implements ClcUserIdentityService {

    @Autowired
    private SysDictDataDao dictDataDao;

    @Override
    public QueryWrapper<ClcUserIdentityEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcUserIdentityEntity> wrapper = new QueryWrapper<>();


        return wrapper;
    }

    @Override
    public List<ClcUserIdentityDTO> getByUserIds(Long... userIds) {
        List<ClcUserIdentityEntity> entities = baseDao.selectList(new QueryWrapper<ClcUserIdentityEntity>()
                .lambda().in(ClcUserIdentityEntity::getUserId, Arrays.asList(userIds)));
        List<ClcUserIdentityDTO> dtos = ConvertUtils.sourceToTarget(entities, ClcUserIdentityDTO.class);
        if (dtos.size() > 0) {
            List<Long> collect = dtos.stream().map(ClcUserIdentityDTO::getIdentityId).distinct().collect(Collectors.toList());
            if (collect.size() > 0) {
                List<SysDictDataEntity> dictDataEntities = dictDataDao.selectList(new QueryWrapper<SysDictDataEntity>()
                        .lambda().in(BaseEntity::getId, collect));
                for (ClcUserIdentityDTO dto : dtos) {
                    SysDictDataEntity dictDataEntity = dictDataEntities.stream().filter(t -> t.getId().equals(dto.getIdentityId())).findFirst().orElse(null);
                    dto.setIdentityName(dictDataEntity == null ? null : dictDataEntity.getDictLabel());
                }
            }
        }

        return dtos;
    }

    @Override
    @Transactional
    public void saveOrUpdate(Long userId, List<Long> identityIdList) {
        //先删除用户身份关系
        deleteByUserIds(new Long[]{userId});

        //用户没有一个身份的情况
        if(CollUtil.isEmpty(identityIdList)){
            return;
        }

        List<ClcUserIdentityEntity> list = new ArrayList<>();
        //保存用户身份关系
        for(Long identityId : identityIdList){
            ClcUserIdentityEntity userIdentityEntity = new ClcUserIdentityEntity();
            userIdentityEntity.setUserId(userId);
            userIdentityEntity.setIdentityId(identityId);

            list.add(userIdentityEntity);
        }
        //保存
        insertBatch(list);
    }

    @Override
    @Transactional
    public void deleteByUserIds(Long[] userIds) {
        baseDao.delete(new QueryWrapper<ClcUserIdentityEntity>()
                .lambda().in(ClcUserIdentityEntity::getUserId, Arrays.asList(userIds)));
    }
}